#ifndef AVALON_TASK_HPP
#define AVALON_TASK_HPP
#pragma warning(disable : 4267)
#include <boost/thread.hpp>
#pragma warning(default : 4267)
#include "Avalon/Threading/Threading.hpp"

namespace Avalon {
namespace Threading {

  /*! \class Task
   *  \brief Interface for a performing a task as part of a thread pool.
   *  \author Kamal Mansouri <kranar@hotmail.com>
   */
  class Task {
    public:

      //! Waits for all Tasks to complete.
      /*!
        \param tasks The Tasks to wait for.
      */
      template<typename T>
      static void WaitAll(const T& tasks) {
        for(typename T::const_iterator i = tasks.begin();
            i != tasks.end(); ++i) {
          (*i)->Wait();
        }
      }

      //! Constructs a Task.
      /*!
        \param deleteWhenFinished Whether this Task will delete itself when
                                  it's finished.
      */
      Task(bool deleteWhenFinished = false);

      virtual ~Task();

      //! Queues this Task in the thread pool.
      void Enqueue();

      //! Waits for the Task to complete.
      void Wait();

    protected:

      //! Processes a single iteration of this Task.
      virtual void ProcessTask() = 0;

      //! Increases the maximum number of concurrent Tasks.
      void IncreaseTaskCount();

      //! Decreases the maximum number of concurrent Tasks.
      void DecreaseTaskCount();

    private:
      class ThreadPool;
      class TaskThread;
      boost::mutex m_mutex;
      boost::condition_variable m_queuedCondition;
      bool m_deleteWhenFinished;
      int m_workCount;
  };
}
}

#endif // AVALON_TASK_HPP
